class Admin::PostsController < ApplicationController
  
  layout 'admin'
  require_role 'admin'

  
  def edit
    @post = Post.find(params[:id])
  end
  
  def destroy
    @post = Post.find(params[:id])
    if @post.destroy
      flash[:success] = 'Ressource destroyed'
    end
    redirect_to :action => 'index'
  end
  
  def create
    @post = Post.new(params[:post])
    if @post.save
      flash[:success] = 'New ressource saved'
      redirect_to 'index'
    else
      render :action => 'new'
    end
  end
  
  def new
    @post = Post.new(params[:post])
  end
  
  def update
    @post = Post.find(params[:id])
    if @post.update_attributes(params[:post])
      flash[:success] = 'Ressource updated'
      redirect_to :action => 'index'
    else
      render :action => 'edit'
    end
  end
  
  def index
    @posts_by_category = Post.find(:all, :order => 'category_id ASC, created_at DESC')
  end

end